<template>
	<view>
		<view class="kong"></view>
		<view class="texs">测评理论说明：</view>
		<view class="conte dis dis1 dis2">
			<text class="texts">{{texts}}</text>
		</view>
		<block v-if="isfree==1 || isbuy==1">
			<!-- <block> -->
			<view class="botmBg" :class="[isIPX?'isIPXs':'']">
				<view class="botm contenr" @click="goUrl">开始测评</view>
				<view class="rmbTexts contenr" v-if="isbuy==1">已购买</view>
			</view>
			
			<!-- <view class="rmbTexts contenr" v-if="isfree==1">免费</view> -->
		</block>
		<block v-if="isfree==0 && isbuy==0">
			<view class="botmom dis dis2 dis3" :class="[isIPX?'isIPXs':'']">
				<text class="text1Bto">￥</text>
				<text class="rmb">{{price}}</text>
				<view class="btns contenr" @click="pay">立即购买</view>
			</view>
		</block>
	</view>
</template>

<script>
	let app = getApp().globalData;
	// let setTime = null;
	import payment from '../../../common/js/pay.js';
	export default {
		data() {
			return {
				id: 0,
				texts: '',
				title: '',
				isbuy: '', //是否支付
				isfree: '', //是否免费
				price: 0, //价格
				setTime: null,
				isIPX: app.isIPX,
			};
		},
		methods: {
			goUrl() {
				uni.redirectTo({
					url: `../../me/answer/answer?id=${this.id}`
				})
			},
			pay() {
				let price = this.price;
				let id = this.id;
				let that = this;
				console.log('id=', id);
				let p = 0;
				if (price <= 0) {
					return
				};
				payment.pay(price, id).then(res => {
					if (res.orderno) {
						let orderno = res.orderno;
						// uni.showLoading({
						// 	title: '正在查询支付结果'
						// });
						payment.wxcheckpay(orderno).then(res => {
							console.log(res)
							let {
								data
							} = res.data;
							if (data.ispay == 1) {
								// uni.hideLoading();
								uni.redirectTo({
									url: `../../me/answer/answer?id=${id}`
								})
							} else {
								that.setTime = setInterval(() => {
									p++
									payment.wxcheckpay(orderno).then(res => {
										console.log(res)
										let {
											data
										} = res.data;
										if (data.ispay == 1) {
											// uni.hideLoading();
											uni.redirectTo({
												url: `../../me/answer/answer?id=${id}`
											})
											clearInterval(that.setTime)
										}
									})
									if (p >= 4) {
										console.log('清除定时器')
										clearInterval(that.setTime)
									}
								}, 2000)
							}

						})
					}
					console.log(res)
				})
			},


		},
		onLoad(e) {
			console.log(e)
			this.id = e.id;
			this.texts = app.desciption ||
				'本测试来自《中医体质分类与判定》国家标准。该标准应用了中医体质学、遗传学、流行病学、心理测量学、数理统计学等多学科交叉的方法，经中医体质专家、临床专家、流行病学专家多次讨论论证而建立，并在全国范围内进行了21948例流行病学调查，显示出良好的适应性、可行性。';
			this.title = e.title;
			this.isbuy = e.isbuy;
			this.isfree = e.isfree;
			this.price = e.price;

		},
		onHide() {
			console.log('sssss')
			this.setTime = null;
			clearInterval(this.setTime)
			console.log('that.setTime', this.setTime)
		},
		onUnload() {
			clearInterval(this.setTime)
			this.setTime = null;
		}

	}
</script>

<style lang="scss">
	@import "../../../common/css/flex.css";
	@import "../../../common/css/index.scss";

	page {
		background-color: #FFFFFF;
	}

	.kong {
		width: 100%;
		height: 101upx;
	}

	.texs {
		width: 686upx;
		height: 62upx;
		font-size: 44upx;
		font-family: $typeface;
		font-weight: 500;
		color: #333333;
		margin: auto;
	}

	.conte {
		width: 686upx;
		min-height: 160upx;
		margin: auto;
		margin-top: 32upx;
		background: #F4F8FF;
		border-radius: 24upx;
		border: 3upx solid #D0DCED;
		padding: 40upx 0upx;

		font-size: 32upx;
		font-family: $typeface1;
		font-weight: 400;
		color: #333333;

		.texts {
			width: 608upx;
			// min-height: 359upx;
		}

		// .shuom{
		// 	width: 686upx;
		// 	height: 449upx;
		// }
	}
	.botmBg{
		width: 100%;
		height:245upx ;
		position: fixed;
		bottom: 0;
		.botm {
			width: 446upx;
			height: 88upx;
			background: #FFD344;
			border-radius: 44upx;
			font-size: 32upx;
			font-family: $typeface;
			font-weight: 500;
			color: #333333;
			margin: auto;
			// margin-top: 392upx;
		}
	}
	

	.rmbTexts {
		width: 100%;
		height: 45upx;
		font-size: 32upx;
		font-family: $typeface1;
		font-weight: 400;
		color: #E85335;
		margin-top: 18upx;
	}

	.botmom {
		width: 100%;
		height: 128upx;
		background: #FFFFFF;
		position: fixed;
		bottom: 0;
		border-top: 1px solid #e9e9e9;

		.text1Bto {
			font-size: 44upx;
			font-family: $typeface;
			font-weight: 500;
			color: #E85335;
			margin-left: 66upx;
			margin-top: 20upx;

		}

		.rmb {
			font-size: 68upx;
			font-family: $typeface;
			font-weight: 500;
			color: #E85335;
		}

		.btns {
			width: 426upx;
			height: 88upx;
			background: #FF5938;
			border-radius: 44upx;
			font-size: 32upx;
			font-family: $typeface;
			font-weight: 500;
			color: #FFFFFF;
			margin-left: 58upx;
			margin-right: 32upx;
		}

	}
	.isIPXs {
		padding-bottom: 68upx;
	}
</style>
